package ttelectronic.mobileholder.service;

import java.util.ArrayList;
import java.util.Set;

import ttelectronic.mobileholder.common.MHCommon;
import ttelectronic.mobileholder.core.MHDeviceDetailObject;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MHAutoConnectionService extends Thread {

	// parameter
	private Context mContext;
	private BluetoothAdapter mBluetoothAdapter;
	private Handler mHandler;

	// member variable
	private ArrayList<MHDeviceDetailObject> mListDevicesPaired;
	private ArrayList<MHDeviceDetailObject> mListDevicesDiscovery;

	public MHAutoConnectionService(Context context,
			BluetoothAdapter bluetoothAdapter, Handler handler) {
		// set parameter
		mContext = context;
		mBluetoothAdapter = bluetoothAdapter;
		mHandler = handler;

		// init member variable
		mListDevicesDiscovery = new ArrayList<MHDeviceDetailObject>();
		mListDevicesPaired = new ArrayList<MHDeviceDetailObject>();

		// register broadcast
		// Register the BroadcastReceiver
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		filter.addAction(BluetoothDevice.ACTION_UUID);
		filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
		filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		mContext.registerReceiver(ActionFoundReceiver, filter);
	}

	@Override
	public void run() {
		super.run();
		while (true) {
			if (!MHCommon.BLUETOOTH_STATUS) {
				mBluetoothAdapter.startDiscovery();
				while (mBluetoothAdapter.isDiscovering()) {

				}
				if (isContent() != null) {
					Log.d(MHCommon.TAG, "Put message");
					Message msg = mHandler
							.obtainMessage(MHCommon.MESSAGE_AUTOCONNECTION);
					Bundle bundle = new Bundle();
					bundle.putString(MHCommon.BUNDLE_MAC_ADDRESS, isContent());
					msg.setData(bundle);
					mHandler.sendMessage(msg);
				}
			}
		}
	}

	@Override
	@Deprecated
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		mContext.unregisterReceiver(ActionFoundReceiver);
	}

	private String isContent() {
		getListDevicesPaired();
		Log.d(MHCommon.TAG + "Paired", mListDevicesPaired.size() + "");
		Log.d(MHCommon.TAG + "Discovery", mListDevicesDiscovery.size() + "");
		for (MHDeviceDetailObject devicePaired : mListDevicesPaired) {
			for (MHDeviceDetailObject deviceDiscovery : mListDevicesDiscovery) {
				if (devicePaired.getDeviceMacAddress().equals(
						deviceDiscovery.getDeviceMacAddress()))
					return devicePaired.getDeviceMacAddress();
			}
		}
		return null;
	}

	// Get list bluetooth devices paired
	public ArrayList<MHDeviceDetailObject> getListDevicesPaired() {
		mListDevicesPaired.clear();
		if (mBluetoothAdapter != null) {
			Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
					.getBondedDevices();
			if (pairedDevices.size() > 0) {
				for (BluetoothDevice device : pairedDevices) {
					mListDevicesPaired.add(new MHDeviceDetailObject(device
							.getName(), device.getAddress(), device
							.getBluetoothClass().getDeviceClass()));
				}
			}
		} else
			Log.d(MHCommon.TAG,
					"MHAutoConnectionService - BluetoothAdapter is null");

		return mListDevicesPaired;
	}

	// Broadcast get list devices discovery
	private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				BluetoothDevice device = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				// Log.d(MHCommon.TAG, "\n  Device: " + device.getName() + ", "
				// + device);
				mListDevicesDiscovery.add(new MHDeviceDetailObject(device
						.getName(), device.getAddress(), device
						.getBluetoothClass().getDeviceClass()));
			}
		}
	};

}
